package edu.sdjzu.exam.mapper;

import edu.sdjzu.exam.domain.course.Course;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface CourseMapper {
    
    @Select("SELECT * FROM courses WHERE course_id = #{courseId}")
    Course findById(Long courseId);
    
    @Select("SELECT * FROM courses WHERE status = 'active' ORDER BY course_name")
    List<Course> findAllActiveCourses();
    
    @Insert("INSERT INTO courses (course_name, course_code, description, subject, created_by) " +
            "VALUES (#{courseName}, #{courseCode}, #{description}, #{subject}, #{createdBy})")
    @Options(useGeneratedKeys = true, keyProperty = "courseId")
    void insert(Course course);
    
    @Update("UPDATE courses SET course_name = #{courseName}, course_code = #{courseCode}, " +
            "description = #{description}, subject = #{subject}, updated_at = NOW() " +
            "WHERE course_id = #{courseId}")
    int update(Course course);
    @Delete("DELETE from courses where course_id =  #{courseId}")
    void deleteCourse(Integer courseId);
    @Update("UPDATE courses SET status = #{status}, updated_at = NOW() WHERE course_id = #{courseId}")
    int updateStatus(@Param("courseId") Long courseId, @Param("status") String status);
    
    // 根据老师ID查询其有权限的课程
    @Select("SELECT c.* FROM courses c " +
            "JOIN teacher_courses tc ON c.course_id = tc.course_id " +
            "WHERE tc.teacher_id = #{teacherId} AND tc.status = 'active' AND c.status = 'active' " +
            "ORDER BY c.course_name")
    List<Course> findCoursesByTeacher(@Param("teacherId") Long teacherId);
    @Select("SELECT * FROM course")
    List<Course> selectCourse();

}